package first.endtoend.tsm

import first.endtoend.tsm.webservices.GlobalEligibility
import first.endtoend.tsm.webservices.GlobalServiceManagement
import first.endtoend.tsm.webservices.MobileSubscription
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.GpFunctions
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.CheckMobileSubscriptionEligibilityRequestType
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.DeclareServiceInstanceReferenceRequestType
import org.springframework.dao.DataIntegrityViolationException

class TSMController
{
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def filterPaneService
    TSMHelperTools tools = new TSMHelperTools()

    def index()
    {
        redirect(action: "list", params: params)
    }

    def list(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        [TSMInstanceList: TSM.list(params), TSMInstanceTotal: TSM.count()]
    }

    def create()
    {
        [TSMInstance: new TSM(params)]
    }

    def save()
    {
        def TSMInstance = new TSM(params)
        if (!TSMInstance.save(flush: true))
        {
            render(view: "/TSM/deletion_create", model: [TSMInstance: TSMInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'TSM.label', default: 'TSM'), TSMInstance.id])
        redirect(action: "show", id: TSMInstance.id)
    }

    def show(Long id)
    {
        def TSMInstance = TSM.get(id)
        if (!TSMInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "list")
            return
        }

        [TSMInstance: TSMInstance]
    }

    def edit(Long id)
    {
        def TSMInstance = TSM.get(id)
        if (!TSMInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "list")

            return
        }

        [TSMInstance: TSMInstance]
    }

    def update(Long id, Long version)
    {
        def TSMInstance = TSM.get(id)
        if (!TSMInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (TSMInstance.version > version)
            {
                TSMInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'TSM.label', default: 'TSM')] as Object[],
                        "Another user has updated this TSM while you were editing")
                render(view: "edit", model: [TSMInstance: TSMInstance])
                return
            }
        }

        TSMInstance.properties = params

        if (!TSMInstance.save(flush: true))
        {
            render(view: "edit", model: [TSMInstance: TSMInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'TSM.label', default: 'TSM'), TSMInstance.id])
        redirect(action: "show", id: TSMInstance.id)
    }

    def delete(Long id)
    {
        def TSMInstance = TSM.get(id)
        if (!TSMInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "list")
            return
        }

        try
        {
            TSMInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'TSM.label', default: 'TSM'), id])
            redirect(action: "show", id: id)
        }
    }


    def filter = {
        if (!params.max) params.max = 10
        render(view: 'list',
                model: [TSMInstanceList : filterPaneService.filter(params, TSM),
                        TSMInstanceTotal: filterPaneService.count(params, TSM),
                        filterParams    : org.grails.plugin.filterpane.FilterPaneUtils.extractFilterParams(params),
                        params          : params])
    }

    /**
     * Delete Suspension use case webservices are :
     * -DeclareServiceInstanceReference (GlobalServiceManagement)
     * -CheckMobileSubsriptionEligibility (MobileSubscription)
     * -SuspendOrResumeState (GlobalServiceManagement)
     * @return
     */
    def delete_suspension()
    {
    }

    /**
     * Installation Activation use case; webservices are :
     * -DeclareServiceInstanceReference (GlobalServiceManagement)
     * -CheckMobileSubsriptionEligibility (MobileSubscription)
     */
    def installation_activation()
    {

    }

    def lock_unlock()
    {

    }

    def upgrade()
    {

    }

    /**
     * Delete Suspension use case webservices are :
     * -DeclareServiceInstanceReference (GlobalServiceManagement)
     * -CheckMobileSubsriptionEligibility (MobileSubscription)
     * -SuspendOrResumeState (GlobalServiceManagement)
     * -HandleStartServiceStateChangeNotification (ServiceCycleNotification)
     * -HandleEndServiceStateChangeNotification (ServiceCycleNotification)
     */
    def deletion_test()
    {
       // GlobalEligibility globalEligibility = new GlobalEligibility(grailsApplication.config.first.endtoend.tsm.globalEligibility)
        GlobalServiceManagement globalServiceManagement = new GlobalServiceManagement(grailsApplication.config.tsm.globalServiceManagement)
      //  MobileSubscription mobileSubscription = new MobileSubscription(grailsApplication.config.first.endtoend.tsm.mobileSubscription)
       // CardContentManagement cardContentManagement = new CardContentManagement(grailsApplication.config.first.endtoend.tsm.cardContentManagement)
       // ServiceCycleNotification cycleNotification = new ServiceCycleNotification()

       //HashMap<String, String> checkMobileResponse = mobileSubscription.checkMobileSubscriptionEligibility()
       HashMap<String, String> declareServiceResponse = globalServiceManagement.declareServiceInstanceReference()
    //   cycleNotification.handleSartServiceStateChange("1")
    //   globalServiceManagement.suspendOrResumeService(true)
    //   cycleNotification.handleEndServiceStateChangeNotification("1")

        [declareServiceResponse:declareServiceResponse]
    }

    def test_global()
    {
       // String URL = "http://vgerndvud1098:7101/Tsm/GlobalEligibilityInfoService"
        //  String URL = grailsApplication.config.first.endtoend.tsm.globalEligibilityInfo
        String URL = "http://GKHBH12:8088/mockGlobalEligibilityInfoBinding"
        println "###### URL ==> " + URL
        GlobalEligibility globalEligibility = new GlobalEligibility(URL)
        HashMap<String, String> checkEligibility = globalEligibility.checkGlobalEligibility()
        [checkEligibility: checkEligibility]
    }

    /**
     * The response is already fill, no webservice call, just for demo
     */
    def test_global_fake()
    {
        GlobalEligibility globalEligibility = new GlobalEligibility(grailsApplication.config.tsm.globalServiceManagement)
        HashMap<String, String> checkEligibility = globalEligibility.checkGlobalEligibilityTest()
        [checkEligibility: checkEligibility]

    }

    /**
     * Delete Suspension use case webservices are :
     * -DeclareServiceInstanceReference (GlobalServiceManagement)
     * -CheckMobileSubsriptionEligibility (MobileSubscription)
     * -SuspendOrResumeState (GlobalServiceManagement)
     * -HandleStartServiceStateChangeNotification (ServiceCycleNotification)
     * -HandleEndServiceStateChangeNotification (ServiceCycleNotification)
     */
    def deletion_response()
    {
        //Request Creation
        CheckMobileSubscriptionEligibilityRequestType checkMobileSubscriptionEligibilityRequestType = new CheckMobileSubscriptionEligibilityRequestType()
        checkMobileSubscriptionEligibilityRequestType.FunctionCallIdentifier = GpFunctions.CheckMobileSubscriptionEligibility
        //Set the Function Identifier
        checkMobileSubscriptionEligibilityRequestType.MobileSubscription = tools.initMobileSubcription(params.mnoName)
        checkMobileSubscriptionEligibilityRequestType.Service = tools.initService(params.serviceID)
        checkMobileSubscriptionEligibilityRequestType.ServiceQualifier = tools.initServiceQualifier(params.serviceQualifierName)

        DeclareServiceInstanceReferenceRequestType declareServiceInstanceReferenceRequestType = new DeclareServiceInstanceReferenceRequestType()
        declareServiceInstanceReferenceRequestType.FunctionCallIdentifier = GpFunctions.DeclareServiceInstanceReference
        declareServiceInstanceReferenceRequestType.ServiceInstanceReference = tools.initServiceReferenceType()
        declareServiceInstanceReferenceRequestType.Service = tools.initService(params.serviceID)
        declareServiceInstanceReferenceRequestType.ServiceQualifier = tools.initServiceQualifier(params.serviceQualifierName)
        declareServiceInstanceReferenceRequestType.MobileSubscription = tools.initMobileSubcription(params.mnoName)

        //Calling Webservices
        MobileSubscription mobileSubscription = new MobileSubscription(grailsApplication.config.tsm.mobileSubscription)
        //Call the GlobalEligibility Class and set it with the URL of checkGlocalEligibility webservice
        GlobalServiceManagement globalServiceManagement = new GlobalServiceManagement(grailsApplication.config.tsm.globalServiceManagement)
        HashMap<String, String> checkMobileResponse = mobileSubscription.checkMobileSubscriptionEligibility(checkMobileSubscriptionEligibilityRequestType)
        HashMap<String, String> declareService = globalServiceManagement.declareServiceInstanceReference(declareServiceInstanceReferenceRequestType)
        [checkMobileResponse: checkMobileResponse, declareService: declareService]
    }

    def tests()
    {

    }
}
